#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年4月27日

@author: chenyongbing
'''
import sys,os,tarfile,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append(os.path.join(current_dir,'../'))
from base.time_method import myTimeMethod
from base.zhimind_datas import myZhimindDatas
from base.retail_datas import myRetailDatas
from datatuning_datas import myDataTuningDatas
from localConfig import myLocalConfig
import logging
class EmployeeRssi(object):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
    
    def openTarFile(self,rawfile_tar):
        output = ''
        if not os.path.exists(rawfile_tar):
            return output
        try:
            tar = tarfile.open(rawfile_tar)
        except:return ''
        for member in tar.getmembers():
            f = tar.extractfile(member)
            out = f.read()
            output = output + out+'\n'
        tar.close()
        return output 
    
    
    def read_employee_rssi_from_rawdata(self,rawpath='',group=None,st=None,et=None,employees=[]):
        datas = []
        if len(employees)==0:
            comp_group  = myRetailDatas.get_comp_group( shop_group=group)
            employees = myZhimindDatas.get_employees( mtype=1,group=comp_group)
        if len(employees)==0:
            return datas
        if len(rawpath)==0 or rawpath=='':
            rawpath = myLocalConfig.rawdata_path
        
        days = myTimeMethod.get_slist_between_st_et(st, et)
        
        for day in days:
            rawfile_tar = rawpath+'/rawdata_group/'+day+'/'+group+'.txt.tar.gz'
            #print rawfile_tar
            if not os.path.exists(rawfile_tar):
                logging.warning( 'rawfile %s not exists.'%rawfile_tar)
                continue
            output = self.openTarFile(rawfile_tar)
            logging.info( "%s,%s"%(rawfile_tar,len(output)))
            for line in output.split('\n'):
                msgLine = line.split('.')
                #print len(msgLine)
                if len(msgLine)!=6 and len(msgLine)!=5:
                    continue
                if len(msgLine)==6:
                    day,dev,dmac,dtime,rssi,rtime = msgLine
                elif len(msgLine)==5:
                    day,dev,dmac,dtime,rssi = msgLine
                if len(dev)>12:continue
                if not re.search('^-[\d]+$',rssi):continue
                if dmac not in employees:continue
                datas.append([day,dev,dmac,rssi])
            logging.info( "found %s"%len(datas))
        return datas

    def stat_rssi_data_by_device_mac(self,datas=[],group=None,st=None,et=None,employees=[]):
        if len(datas)==0:
            datas = self.read_employee_rssi_from_rawdata(group=group, st=st, et=et,employees=employees)
        rssi_datas = []
        devDict = {}
        for data in datas:
            rssi = data[-1]
            device = data[1]
            mac = data[2]
            if device not in devDict.keys():
                devDict[device] = {}
            if mac not in devDict[device].keys():
                rssiDict  = {}
            else:
                rssiDict = devDict[device][mac]
            if rssi not in rssiDict.keys():
                rssiDict[rssi] = 0
            rssiDict[rssi]+=1
            devDict[device][mac] = rssiDict
        return devDict
   
    def stat_rssi_data_by_device(self,datas=[],group=None,st=None,et=None,employees=[]):
        if len(datas)==0:
            datas = self.read_employee_rssi_from_rawdata(group=group, st=st, et=et,employees=employees)
        rssi_datas = []
        devDict = {}
        for data in datas:
            rssi = data[-1]
            device = data[1]
           
            if device not in devDict.keys():
                
                rssiDict  = {}
            else:
                rssiDict = devDict[device]
            if rssi not in rssiDict.keys():
                rssiDict[rssi] = 0
            rssiDict[rssi]+=1
            devDict[device] = rssiDict
        return devDict
    
    def stat_rssi_data(self,datas=[],group=None,st=None,et=None,employees=[]):
        if len(datas)==0:
            datas = self.read_employee_rssi_from_rawdata(group=group, st=st, et=et,employees=employees)
        rssi_datas = []
        rssiDict = {}
        for data in datas:
            rssi = int(data[-1])
            if rssi not in rssiDict.keys():
                rssiDict[rssi] = 0
            rssiDict[rssi]+=1
#         for key,value in rssiDict.iteritems():
#             rssi_datas.append( [str(key),str(value)])
        return rssiDict
    
    
    def get_excluding_rssi_by_order(self,excluding=20,rssiDict={},reverse=False):
        total_count = sum(rssiDict.values())
        excluding_count = int(total_count * int(excluding) /100)
        keys = rssiDict.keys()
        
        if reverse:
            keys.sort()
            keys.reverse()
        else:
            keys.sort()
    
        total = 0
        ototal = 0
        okey = keys[0]
      
        for key in keys:
            total += rssiDict[key]
            fa =abs(ototal - excluding_count)
            fb = abs(total - excluding_count)
            if fa<=fb and total!=0 and total >excluding_count :
                return okey
            elif total >excluding_count:
                return key
            
            ototal = total
            okey = key
    
    def get_excluding_rssi(self,excluding=20,rssiDict={}):
        if len(rssiDict)==0:return '-','-'
        min_rssi = self.get_excluding_rssi_by_order(excluding, rssiDict)
        max_rssi = self.get_excluding_rssi_by_order(excluding, rssiDict,reverse=True)
        return min_rssi,max_rssi
        
        
    
    def get_avg_rssi(self,rssiDict={},max_rssi=None,min_rssi=None,excluding=20):
        if len(rssiDict)==0:return '-'
        if max_rssi==None or min_rssi==None:
            min_rssi,max_rssi = self.get_excluding_rssi(excluding=excluding, rssiDict=rssiDict)
        rssiList = []
        for rssi in rssiDict.keys():
            if rssi<=min_rssi or rssi>=max_rssi:
                continue
            rssiList.append(int(rssi))
#         for rssi in rssiDict.keys():
#             if rssi <=min_rssi or rssi >= max_rssi:
#                 del rssiDict[rssi]
#         min_rssi_2 = self.get_excluding_rssi_by_order(excluding,rssiDict)
#         
#         for rssi in rssiDict.keys():
#             if rssi >min_rssi_2:
#                 continue
#             rssiList.append(int(rssi))
        if len(rssiList)==0:
            return '-'
        else:
            return int(sum(rssiList)/len(rssiList))
    
    
    def get_rssi_from_employee_by_devices(self,devices=[],startTime=None,endTime=None,filterRatio=20,useSysEmployee=0):
#         macDict = myDataTuningDatas.search_employee_mac_rssi( devices=devices , startTime=startTime,endTime=endTime,useSysEmployee=useSysEmployee)
#         rssiDict = {}
#         for mac , mvalues in macDict.items():
#             for rssi,count in mvalues.items():
#                 if rssi not in rssiDict.keys():
#                     rssiDict[rssi] = 0
#                 rssiDict[rssi]+=count
        rssiDict = myDataTuningDatas.search_employee_rssi(devices=devices , startTime=startTime,endTime=endTime,useSysEmployee=useSysEmployee)
        if len(rssiDict)==0:
            desc = '无员工数据'
            avg_rssi = '-'
        elif sum(rssiDict.values())<200:
            avg_rssi = '-'
            desc = '员工数据不足'
        else:
            avg_rssi = self.get_avg_rssi(rssiDict=rssiDict,  excluding=filterRatio)
            if avg_rssi == '-':
                desc ='员工数据不足'
            else:
                desc = ''
        return avg_rssi,desc
    
    def get_rssi_from_employee_by_group(self,groups=[],startTime=None,endTime=None,filterRatio=20,useSysEmployee=0):
        datas = []
        for group in groups:
            devices = myRetailDatas.get_devices_by_groups(groups=[group])
            if useSysEmployee == 1:
                avg_rssi,desc = self.get_rssi_from_employee_by_devices(devices=devices, startTime = startTime, endTime = endTime, filterRatio=filterRatio, useSysEmployee=0)
                if avg_rssi == '-':
                    avg_rssi,desc = self.get_rssi_from_employee_by_devices(devices=devices, startTime = startTime, endTime = endTime, filterRatio=filterRatio, useSysEmployee=1)
            else:
                avg_rssi,desc = self.get_rssi_from_employee_by_devices(devices=devices, startTime = startTime, endTime = endTime, filterRatio=filterRatio, useSysEmployee=useSysEmployee)
            datas.append({'group':group,'rssi':avg_rssi,'desc':desc})
        return datas
    
myEmployeeRssi = EmployeeRssi()
   
if __name__ == '__main__':

    import argparse,re,datetime,time
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--rawpath',metavar=u"rawpath",default='/data/zmexport/rawdata/rawdata_group')
    parser.add_argument('--startTime',metavar=u"starttime",default=None)
    parser.add_argument('--endTime',metavar=u"endtime",default=None)
    parser.add_argument('--shop_group',metavar=u"group",default=None)
#     parser.add_argument('--comp_group',metavar=u"group",default=None)
    args = parser.parse_args()
    startTime = args.startTime
    endTime = args.endTime
    shop_group = args.shop_group
    rawpath = args.rawpath
#     comp_group = args.comp_group
#     if shop_group!=None:
#         comp_group = myRetailDatas.get_comp_group(shop_group = shop_group)
#     
#         employees = myZhimindDatas.get_employees( mtype=1,group=comp_group)
#     else:
#         employees = myZhimindDatas.get_employees( mtype=1)
    
    if shop_group==None:shop_group = '32010394'
    comp_group = myRetailDatas.get_comp_group(shop_group = shop_group)
    if startTime==None:startTime ="2015-01-01"
    if endTime==None:endTime = '2015-01-01'
    

    
    datas = myEmployeeRssi.stat_rssi_data( group=str(shop_group), st=startTime, et=endTime)
    print len(datas)

#     datas = {'-74': 139, '-88': 8, '-86': 7, '-84': 5, '-82': 4, '-80': 12, '-32': 123, '-60': 302, '-62': 358, '-64': 393, '-66': 403, '-68': 371, '-40': 42, '-46': 177, '-44': 116, '-34': 121, '-72': 188, '-90': 1, '-38': 26, '-48': 245, '-56': 302, '-28': 14, '-76': 51, '-54': 242, '-70': 266, '-50': 170, '-52': 188, '-36': 60, '-78': 26, '-58': 304, '-30': 94, '-42': 88}
#     
    for key,value in datas.iteritems():
        print key,value
    
    min_rssi, max_rssi =  myEmployeeRssi.get_excluding_rssi(excluding=20, rssiDict=datas)
    print  min_rssi, max_rssi
    print myEmployeeRssi.get_avg_rssi(rssiDict=datas, max_rssi=max_rssi, min_rssi=min_rssi)
    
        
    